package cn.com.fssg.platform.base.handler.impl.cl.fuju;

import cn.com.fssg.platform.base.handler.InterfaceHandler;
import cn.com.fssg.platform.biz.BizUtil;
import cn.com.fssg.platform.biz.ExtendUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;


@Component
public class ClFujuselectHandler implements InterfaceHandler {
    private NamedParameterJdbcTemplate jdbcTemplate = BizUtil.cur().tenantMainJdbcTemplate();
    // @Autowired
    //private NamedParameterJdbcTemplate jdbcTemplate;
    private SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object run(HttpServletRequest request, JSONObject jsonObject) throws Exception {
        String type = jsonObject.getString("type");
        if(StringUtils.equals(type, "0")){
            //通过当前用户查询机构
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("id",BizUtil.cur().curUserId());
            //queryMap.put("id","777517794022252544");
            String sql = "SELECT *  FROM pxone.su_user WHERE id =:id";
            Map<String,Object> userMap = new HashMap<>();
            userMap = jdbcTemplate.queryForMap(sql,queryMap);
            List<Map<String,Object>> fjList = new ArrayList<>();
            if(!userMap.isEmpty()){
                Map<String,Object> queryFjMap = new HashMap<>();
                if(ObjectUtils.isNotEmpty(userMap.get("phone"))){
                    queryFjMap.put("cjr_jhr_phone",userMap.get("phone"));
                }else{
                    queryFjMap.put("cjr_jhr_phone",userMap.get("loginname"));
                }
                String fjSql = "SELECT * FROM cl_apply_fuju WHERE cjr_jhr_phone =:cjr_jhr_phone";
                fjList = jdbcTemplate.queryForList(fjSql,queryFjMap);
            }
            return fjList;

        }else if(StringUtils.equals(type, "1")){
            //通过当前登陆用户查询机构
            Map<String,Object> queryMap = new HashMap<>();
            queryMap.put("id",BizUtil.cur().curUserId());
            //queryMap.put("id","763780717736747008");
            String sql = "SELECT cl.* FROM cl_org cl LEFT JOIN cl_org_user_customer us ON us.org_id = cl.id where us.user_id =:id";
            List<Map<String,Object>> orgList = new ArrayList<>();
            Map<String,Object> orgMap = new HashMap<>();
            orgList = jdbcTemplate.queryForList(sql,queryMap);
            if(orgList.size()>0){
                orgMap = orgList.get(0);
            }
            return orgMap;
        }
        return null;
    }


}
